package cn.jiyun.function.config;

import cn.jiyun.function.mapper.DemandMapper;
import cn.jiyun.function.service.DemandService;
import com.alibaba.nacos.common.packagescan.resource.ClassPathResource;
import com.alibaba.nacos.common.packagescan.resource.Resource;
import com.github.houbb.heaven.util.lang.CharUtil;
import com.github.houbb.sensitive.word.api.ISensitiveWordReplace;
import com.github.houbb.sensitive.word.api.ISensitiveWordReplaceContext;

import com.github.houbb.sensitive.word.api.IWordDeny;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;

import com.github.houbb.sensitive.word.support.deny.WordDenys;


//import org.springframework.context.annotation.Bean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

//@Component
@Slf4j
@Configuration

public class SensitiveConfig {
    private final DemandService demandService;

    public SensitiveConfig(DemandService demandService) {
        this.demandService = demandService;
    }

    // 配置默认敏感词 + 自定义敏感词

    @Bean
    public SensitiveWordBs sensitiveWordBs() {
 CustomWordDeny customWordDeny = new CustomWordDeny(demandService);
        IWordDeny wordDeny = WordDenys.chains(
                WordDenys.system() == null? null : WordDenys.system(),
                customWordDeny);
        SensitiveWordBs sensitiveWordBs = SensitiveWordBs.newInstance();
        return sensitiveWordBs
                .ignoreCase(true)
                // 忽略半角圆角
                .ignoreWidth(true)
                // 忽略数字的写法
                .ignoreNumStyle(true)
                // 忽略中文的书写格式：简繁体
                .ignoreChineseStyle(true)
                // 忽略英文的书写格式
                .ignoreEnglishStyle(true)
                // 忽略重复词
                .ignoreRepeat(false)
                // 是否启用数字检测
                .enableNumCheck(true)
                // 是否启用邮箱检测
                .enableEmailCheck(true)
                // 是否启用链接检测
                .enableUrlCheck(true)
                // 数字检测，自定义指定长度
                .numCheckLen(8)
                // 配置自定义敏感词
                .wordDeny(wordDeny)
                .init();
    }
}

//    public class MySensitiveWordReplace implements ISensitiveWordReplace {
//        @Override
//        public String replace(ISensitiveWordReplaceContext context) {
//            String sensitiveWord = context.sensitiveWord();
//            if ("五星红旗".equals(sensitiveWord)) {
//                return "国家旗帜";
//            }
//            if ("毛主席".equals(sensitiveWord)) {
//                return "教员";
//            }
//            int wordLength = context.wordLength();
//            return CharUtil.repeat('*', wordLength);
//        }
//    }


//    @Slf4j
//    public class MyWordAllow implements IWordAllow {
//
//        @Override
//        public List<String> allow() {
//            List<String> list = new ArrayList<>();
//            try {
//                Resource myAllowWords = new ClassPathResource("myNotSensitiveWords.txt");
//                Path myAllowWordsPath = Paths.get(myAllowWords.getFile().getPath());
//                list = Files.readAllLines(myAllowWordsPath, StandardCharsets.UTF_8);
//            } catch (IOException ioException) {
//                log.error("读取非敏感词文件错误！" + ioException.getMessage());
//            }
//            return list;
//        }
//
//    }

//@Slf4j
//    public class CustomWordDeny implements IWordDeny {
//
//    @Override
//    public List<String> deny() {
//        List<String> list = new ArrayList<>();
//        try {
//            Resource mySensitiveWords = new ClassPathResource("mySensitiveWords.txt");
//            Path mySensitiveWordsPath = Paths.get(mySensitiveWords.getFile().getPath());
//            list = Files.readAllLines(mySensitiveWordsPath, StandardCharsets.UTF_8);
//        } catch (IOException ioException) {
//            log.error("读取敏感词文件错误！" + ioException.getMessage());
//        }
//        return list;
//    }
//}}

//    @Override
//    public List<String> deny() {
//        String sql = "SELECT badword FROM demand_sensitive_word";
//        try {
//            return jdbcTemplate.queryForList(sql, String.class);
//        } catch (Exception e) {
//            log.error("读取敏感词从数据库错误！", e);
//            return null;
//        }
//    }
//}
//        @Override
//        public List<String> deny() {
//            List<String> list = new ArrayList<>();
//            try {
//
//                Resource mySensitiveWords = new ClassPathResource("mySensitiveWords.txt");
//                Path mySensitiveWordsPath = Paths.get(mySensitiveWords.getFile().getPath());
//                list = Files.readAllLines(mySensitiveWordsPath, StandardCharsets.UTF_8);
////               list= demandMapper.findAll();
//            } catch (IOException ioException) {
//                log.error("读取敏感词文件错误！" + ioException.getMessage());
//            }
//            return list;
//        }
//
//    }


//}







